<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src=".././static/js/public-head.js"></script>
</head>
<body>
<div class="layui-card" >
    <div class="layui-card-body"  >
        <form class="layui-form  layui-form-pane" action="" lay-filter="cfg-form">
            <input type="hidden" name="id"  value="">
            <div class="layui-form-item">
                <label class="layui-form-label">网站名称</label>
                <div class="layui-input-inline" >
                    <input type="text" name="webName" readonly lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">网站LOGO</label>
                <div class="layui-input-inline" >
                    <button type="button" class="layui-btn sys-upload"  field="webIcon" value="favicon.ico">
                        <i class="layui-icon layui-icon-upload"></i>上传图片
                    </button>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <a href="#" class="cfg-img-view" _img="favicon.ico" field="webIcon"><i class="layui-icon layui-icon-search"></i>预览</a>
                    建议图片尺寸：20px * 20px
                </div>
                <div class="layui-input-inline" >
                    <input type="checkbox" lay-filter="cfg-img-del"  _img="favicon.ico" field="webIcon" name="webIcon" lay-skin="switch" lay-text="开启|关闭">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">系统名称</label>
                <div class="layui-input-inline" style="width: 60%">
                    <input type="text" name="sysName" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">系统LOGO</label>
                <div class="layui-input-inline" >
                    <button type="button" class="layui-btn sys-upload"  field="sysLogo" value="logo.png">
                        <i class="layui-icon layui-icon-upload"></i>上传图片
                    </button>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <a href="#" class="cfg-img-view" _img="logo.png" field="sysLogo"><i class="layui-icon layui-icon-search"></i>预览</a>
                    建议图片尺寸：150px * 45px
                </div>

                <div class="layui-input-inline" >
                    <input type="checkbox" lay-filter="cfg-img-del" _img="logo.png" field="sysLogo" name="sysLogo" lay-skin="switch" lay-text="开启|关闭">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">首页名称</label>
                <div class="layui-input-inline" style="width: 60%">
                    <input type="text" name="indexLinkName" lay-verify=" " autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    右上角链接名称
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">首页链接</label>
                <div class="layui-input-inline"  style="width: 60%">
                    <input type="text" name="indexLink" lay-verify=" " autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    右上角链接
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">文件类型</label>
                <div class="layui-input-inline" style="width: 60%">
                    <input type="text" name="uploadType" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    上传文件类型限制
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" >文件大小</label>
                <div class="layui-input-inline" style="width: 60%">
                    <input type="text" name="maxUpload" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    上传文件大小限制(MB)
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" >登陆时长</label>
                <div class="layui-input-inline" style="width: 60%">
                    <input type="text" name="tokenTime" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    用户登陆时长(分钟)
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">邮箱账号</label>
                <div class="layui-input-inline" style="width: 60%">
                    <input type="text" name="emailUser"  autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    发件人邮箱账号
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱密码</label>
                <div class="layui-input-inline" style="width: 60%">
                    <input type="text" name="emailPassWord"   autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">

                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱Host</label>
                <div class="layui-input-inline" style="width: 60%">
                    <input type="text" name="emailHose"  autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    邮箱服务器地址
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">CopyRight</label>
                <div class="layui-input-block" style="width: 60%">
                    <input type="text" name="copyRight" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>


            <div class="layui-form-item layui-layout-admin">
                <div class="layui-input-block">
                    <div class="layui-footer" style="left: 0;">
                        <button class="layui-btn" lay-submit="" lay-filter="cfg-form-bt">保存</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
</html>

<script type="text/javascript">
    var layindex,layevent;
    layui.use([ 'form','upload'], function() {
        var table = layui.table;
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;
        var upload = layui.upload

        var value ;
        var field;
        var uploadInst = upload.render({
            elem: '.sys-upload' //绑定元素
            ,url:basePath+ '/sysViewCfg/upLoad' //上传接口
            ,number:1
            ,size:500
            ,acceptMime: 'image/*'
            ,data: {token:token
                ,id:function(){
                    return $('input[name="id"]').val()
                }
                ,field:function(){
                    return field
                }
                ,value:function(){
                    return  value
                }
            }
            ,done: function(rdata, index, upload){
                if(rdata.code == 0){
                    localStorage.setItem(field,value)

                    if(field == "sysLogo"){
                        form.val("cfg-form",{
                            "sysLogo":true
                        })
                        $(parent.document).find(".logo").html("<a href='#'><img src='"+ basePath+"/static/images/"+value+"' style='width:200px;height:45px;float:left' ></a>")

                    }else if(field == "webIcon"){
                        form.val("cfg-form",{
                            "webIcon":true
                        })
                    }

                }
                layer.msg(rdata.msg)
            }
            ,error: function(index,upload){
                layer.msg("上传失败,可能网络存在异常..")
            }

        });

        $(".sys-upload").click(function(e){
            value = $(this).attr("value")
            field = $(this).attr("field")
        })

        $(".cfg-img-view").click(function(e){
            var img = $(this).attr("_img");
            layer.open({
                type:1,
                title:false ,
                scrollbar:false,
                shadeClose:true,
                closeBtn:0,
                skin:'layui-layer-nobg',
                content:" <img style='max-height:100px;max-width:300px' src='"+basePath+"/static/images/"+img+"'>"
            })
        })

        form.on("switch(cfg-img-del)",function(data){
            var domData = data.elem;
            var checked = domData.checked;
            var field = $(this).attr("field");
            var value1 = $(this).attr("_img");
            if(!checked){
                value1 = ""
            }
            var param = {field:field,value:value1,token:token}
            $.ajax(basePath+"/sysViewCfg/edit",{
                dataType:"json",
                type:"post",
                data:param,
                success:function(rdata){
                    layer.msg(rdata.msg)
                    if(rdata.code == 0){
                        if(!checked){
                            localStorage.removeItem(field)
                            if(field == "sysLogo"){
                                $(parent.document).find(".logo").html("<a href='#'>"+localStorage.getItem("sysName")+"</a>")
                            }
                        }else{
                            localStorage.setItem(field,value1)
                            if(field == "sysLogo"){
                                $(parent.document).find(".logo").html("<a href='#'><img src='"+ basePath+"/static/images/"+value1+"' style='width:200px;height:45px;float:left' ></a>")
                            }
                        }
                    }else{
                        domData.checked = !checked;
                    }
                }
            });
        })


        form.on("submit(cfg-form-bt)",function(obj){
            var param = obj.field;
            param.token = token;
            $.ajax(basePath+"/sysViewCfg/update",{
                dataType:"json",
                type:"post",
                data:param,
                success:function(rdata){
                    if(rdata.code ==0){
                        localStorage.setItem("headTitle",rdata.data.webName)
                        localStorage.setItem("sysName",rdata.data.sysName)
                        localStorage.setItem("copyRight",rdata.data.copyRight)
                        localStorage.setItem("sysLogo",rdata.data.sysLogo)
                        localStorage.setItem("webIcon",rdata.data.webIcon)
                        localStorage.setItem("indexLink",rdata.data.indexLink)
                        localStorage.setItem("indexLinkName",rdata.data.indexLinkName)
                        localStorage.setItem("uploadType",rdata.data.uploadType)
                        localStorage.setItem("maxUpload",rdata.data.maxUpload)
                        $(parent.document).find(".to-index>a").html(rdata.data.indexLinkName)

                        if(rdata.data.sysLogo == null || rdata.data.sysLogo == ""){
                            $(parent.document).find(".logo").html("<a href='#'>"+localStorage.getItem("sysName")+"</a>")
                        }
                    }
                    layer.msg(rdata.msg)
                }
            });
            return false;
        })

        $.ajax(basePath+"/sysViewCfg/getOne",{
            dataType:"json",
            data:{token:token},
            method:"get",
            success:function(rdata){
                //console.log(JSON.stringify(rdata))
                if(rdata.code == 0){
                    var sysLogo = rdata.data.sysLogo
                    var webIcon = rdata.data.webIcon

                    form.val("cfg-form",rdata.data)
                    form.val("cfg-form",{
                        "switch[sysLogo]":function(){return sysLogo?true:false},
                        "switch[webIcon]":function(){return webIcon?true:false}
                    })
                }
            }
        });


    })
</script>